package com.hzdesk.media.koreabatchmatvector.contral;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.PathBean;
import com.hzdesk.media.koreabatchmatvector.bean.SerPath;
import com.hzdesk.media.koreabatchmatvector.bean.SerPoint;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.inter.ISketchpadDraw;

import java.util.Date;

/**
 * @author Mr.Lee
 */
public class RectuCtl implements ISketchpadDraw {

    private Paint mPaint = new Paint();
    private boolean m_hasDrawn = false;

    private float startx = 0;
    private float starty = 0;
    private float endx = 0;
    private float endy = 0;
    private Path path = new Path();
    private Path mPath;
    private boolean status = false;
    private String color;
    private int size;
    private SerPath mCurPath;
    private long time;
    private int num;
    private Context mContext;

    public RectuCtl(int penSize, String penColor, Context context) {
        mContext = context;
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.parseColor("#" + penColor));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(penSize);
        this.color = penColor;
        this.size = penSize;
    }

    public RectuCtl(Paint paint, Path path) {
        mPaint = paint;
        mPath = path;
        status = true;
    }

    public RectuCtl(Paint paint) {
        mPaint = paint;
    }

    public void draw(Canvas canvas) {
        if (status) {
            canvas.drawPath(mPath, mPaint);
            mPath = null;
            status = false;
        }
        // TODO Auto-generated method stub
        if (null != canvas) {
            //canvas.drawRect(startx, starty, endx, endy, mPaint);

            float x=endx-startx;
            float y=endy-starty;
            if (x>=0&&y>=0) {
                canvas.drawRect(startx, starty, endx, endy, mPaint);
            } else if (x>=0&& y<=0) {
                canvas.drawRect(startx, endy, endx, starty, mPaint);
            } else if (x<0 && y<0) {
                canvas.drawRect(endx, endy, startx, starty, mPaint);
            } else if (x<0 && y>0) {
                canvas.drawRect(endx, starty, startx, endy, mPaint);
            }
        }


    }

    public boolean hasDraw() {
        // TODO Auto-generated method stub
        return m_hasDrawn;
        // return false;
    }

    public void cleanAll() {
        // TODO Auto-generated method stub

    }

    public void touchDown(float x, float y) {
        // TODO Auto-generated method stub
        startx = x;
        starty = y;
        endx = x;
        endy = y;
        num++;
        if (MyClientApplication.isRecording()) {
            time = getTime();
            mCurPath = new SerPath();
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
        }
    }

    public void touchMove(float x, float y) {
        // TODO Auto-generated method stub
        endx = x;
        endy = y;
        m_hasDrawn = true;
        num++;
        if (MyClientApplication.isRecording()) {
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
        }
    }

    public void touchUp(float x, float y) {
        // TODO Auto-generated method stub
        endx = x;
        endy = y;
        num++;
        Log.d("Rect", "size:" + num);
        if (MyClientApplication.isRecording() && MyClientApplication.getMouseUserDeviceinfoBean(mContext, MyClientApplication.getUserNum()).getDisMouse() != 1) {
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
            Log.d("Rect", "size:" + mCurPath.mPoints.size());
            MyClientApplication.setmPathBeanList(new PathBean(time, AppConfig.PAINT_RECT, color, size, mCurPath, MyClientApplication.getUserNum()));
        }
    }

    public long getTime() {
        return new Date().getTime() - MyClientApplication.getStartTime();
    }

}
